﻿using Modules.Corp;
using Modules.Person;
using Modules.Project;
using PmSoft;
using PmSoft.Entity;

namespace ManagementApi.Models.Manage.ProjectGroupMember
{
    public class ProjectGroupMemberExportDto
    {
        public string 项目名称 { get; set; }

        public string 项目编号 { get; set; }

        public string 参建单位 { get; set; }


        public string 班组名称 { get; set; }

        public string 是否项目管理部 { get; set; }

        public string 班组进场日期 { get; set; }

        public string 班组离场日期 { get; set; }

        public string 工人姓名 { get; set; }

        public string 证件号码 { get; set; }

        public string 是否班组长 { get; set; }

        public string 是否签订劳动合同 { get; set; }

        public string 工种 { get; set; }

        public string 联系电话 { get; set; }

        public string 人员进场日期 { get; set; }

        public string 人员离场日期 { get; set; }


    }

    public static partial class ProjectGroupMemberExportExceptions
    {
        /// <summary>
        /// 将数据库模型转换成导出excel模型
        /// </summary>
        /// <param name="members"></param>
        /// <returns></returns>
        public static IEnumerable<ProjectGroupMemberExportDto> AsExportDtos(this IEnumerable<Modules.Project.Entitys.ProjectGroupMember> members)
        {
            var projectGroupService = ServiceAccessor.GetService<ProjectGroupService>();
            var projectGroups = projectGroupService.GetProjectGroups(members.Select(a => a.GroupId));

            var projectService = ServiceAccessor.GetService<ProjectService>();
            var projects = projectService.GetProjectInfoByProjectIds(members.Select(a => a.ProjectId));

            var personService = ServiceAccessor.GetService<PersonService>();
            var persons = personService.GetPersons(members.Select(a => a.PersonId));

            var corpService = ServiceAccessor.GetService<CorpService>();
            var corps = corpService.GetCorpsByCorpIds(members.Select(a => a.CorpId));

            var personContractService = ServiceAccessor.GetService<PersonCorpContractService>();
            var personCorpContracts = personContractService.GetPersonCorpContractsByPersonIds(members.Select(a => a.PersonId));
            List<ProjectGroupMemberExportDto> dtos = new();
            foreach (var item in members)
            {
                var projectGroup = projectGroups.FirstOrDefault(a => a.GroupId == item.GroupId);
                ProjectGroupMemberExportDto dto = new()
                {
                    项目名称 = projects.FirstOrDefault(a => a.ProjectId == item.ProjectId)?.Name ?? string.Empty,
                    项目编号 = projects.FirstOrDefault(a => a.ProjectId == item.ProjectId)?.PrjNum ?? string.Empty,
                    参建单位 = corps.FirstOrDefault(a => a.CorpId == item.CorpId)?.Name ?? string.Empty,
                    班组名称 = projectGroup?.Name ?? string.Empty,
                    是否项目管理部 = projectGroup?.IsManagerGroup == true ? "是" : "否",
                    班组进场日期 = projectGroup?.StartDate?.ToString("yyyy-MM-dd") ?? string.Empty,
                    班组离场日期 = projectGroup?.EndDate?.ToString("yyyy-MM-dd") ?? string.Empty,
                    工人姓名 = persons.FirstOrDefault(a => a.PersonId == item.PersonId)?.Name ?? string.Empty,
                    证件号码 = persons.FirstOrDefault(a => a.PersonId == item.PersonId)?.CardNumber ?? string.Empty,
                    是否班组长 = item.IsLeader == true ? "是" : "",
                    是否签订劳动合同 = personCorpContracts.FirstOrDefault(a => a.CorpId == item.CorpId && a.PersonId == item.PersonId) == null ? "已签订" : "未签订",
                    工种 = item.Type?.GetDescription()??"",
                    联系电话 = persons.FirstOrDefault(a => a.PersonId == item.PersonId)?.Mobile ?? string.Empty,
                    人员进场日期 =item.StartDate?.ToString("yyyy-MM-dd")??"",
                    人员离场日期 =  item.EndDate?.ToString("yyyy-MM-dd") ?? string.Empty
                };
                dtos.Add(dto);
            }

            return dtos;
        }
    }
}
